


/**/
/**/
var/Demo=1

/************************************************************
Litter creates 100,000 random objects on the map.
You can change that to 1 mill if you feel like stress testing
************************************************************/
/*mob/verb/Litter()
	var/Number=100000 //How many to create
	Mark()
	var/Typesof=typesof(/obj)
	for(var/i=1,i<=Number,i++)
		var/Type=pick(Typesof)
		new Type(locate(rand(1,world.maxx),rand(1,world.maxy),z))
	Report()
	*/


/******************************************************/
/* This is for keeping track of how long actions take */
var/Time=0
proc/Mark()
	Time=world.timeofday
proc/Report()
	world<<"Time Taken: [(world.timeofday-Time)/10] Seconds"
/******************************************************/

Host/var/Map_Save/SB

/******/
/*Save*/
/******/

Host/verb/Save()
	set category = "Save"
	Mark()
	SB=new /Map_Save
	SB.Save()
	Report()

/******/
/*Load*/
/******/
Host/verb/Load()
	set category = "Save"
	Mark()
	SB.Load()
	Report()

/*Export to savefile*/
Host/verb/Dump()
	set category = "Save"
	Mark()
	SB.Dump("Demo.sav")
	Report()

/*Import from savefile*/
Host/verb/Import()
	set category = "Save"
	Mark()
	var/Map_Save/A=new
	SB=A
	A.Import("Demo.sav")
	Report()

Host/verb/DeleteAll()
	set category = "Save"
	Mark()
	for(var/atom/A)
		if(istype(A,/mob)) continue
		if(istype(A,/area)) continue
		del A
	Report()












/*******************************************************/
/*******************Map Handling************************/
/*************************By AJX************************/
/*******************************************************/
/*
Version History:
0.2: Added Map Handling Extended, for saving in situations where there are more than 255
different types. However after testing the functionality of this I strongly advise against
ever using it... Moreso releasing it for completeness's sake.
0.1: First public release

*/
/*
Notes:
The use here is pretty straitforward. Save(turf/Loc,Width,Height) and Load(turf/Loc) will
convert the map into text or vice-versa. Loc refers to the farthest lower left point you want
saved. Width and Height are self explanatory. Load will automatically load to the size of the saved file.
Do not try loading a map that will go outside of your maxx/maxy bounds because I didn't include a failsafe.
Use common sense there.

Dump(Filename) and Import(Filename) will save/load the map datum into a savefile format.

I will probably modify this code to a 2 byte setup that will allow 65025 different types of objects to be saved
instead of the current 255 limit, but for the time being you can not have more than 255 different types saved.
This means
/mob is one instance
/obj is another
/obj/Tree is another
/obj/Tree2, /obj/Rock, etc.

This does *NOT* save variables, only the raw types.


*/

/***************************************************/
/* Set this to 1 if you DONT want the object saved */
atom/var/A_DoNotSave=0

/***************************************************/

mob/A_DoNotSave=1
/*Mobs by default are not saved. If you change this*/
/*be sure to make player mobs not be saved manually*/
/***************************************************/


/************************************/
/*  Don't touch from this point on  */
/*Unless you know what you are doing*/
/************************************/


/*******/


Map_Save
	var
		name="Blueprint"
		Map[0]
		Key[0]


	proc/IndexFind(atom/A)
		return Key.Find(A.type)

	proc/IndexType(Num)
		return Key[Num]

	proc/Index(atom/A)
		var/V=IndexFind(A)
		if(V) return V
		if(Key.len==255)
			world<<"\red AJX Debug: 255 Key Limit, Error"
			return 0
		Key+=A.type
		return Key.len

	proc/Import(Filename)
		if(!Filename)
			world<<"\red Map Handling Debug: Include a file name"
			return
		var/savefile/F=new(Filename)
		F["Name"]>>name
		F["Map"]>>Map
		F["Key"]>>Key

	proc/Dump(Filename)
//		for(var/i=1,i<=Map.len,i++)
//			world<<"[Map[i]]"
//		for(var/i=1,i<=Key.len,i++)
//			world<<"[Key[i]]"
		if(!Filename)
			world<<"\red Map Handling Debug: Include a file name"
			return
		var/savefile/F=new(Filename)
		F["Name"]<<name
		F["Map"]<<Map
		F["Key"]<<Key
//		F.ExportText("/","Test.txt")

	proc/Load(turf/Loc)
		var/C
		var/Text
		var/Length
		var/Type
		var/X

		if(!Loc)Loc=locate(1,1,1)

		for(var/i=0,i<Map.len,i++)
			Text=Map[i+1]
			Length=lentext(Text)
			X=-1
			for(var/a=1,a<=Length,a++)
				C=copytext(Text,1,2)
				Text=copytext(Text,2)
				Type=IndexType(text2ascii(C))
				if(findtext("[Type]","/turf",1,6)) X++
				new Type(locate(Loc.x+X,Loc.y+i,1))



	proc/Save(turf/Loc,Width,Height)	//Loc must be lower left corner
		if(!Loc)
			Loc=locate(1,1,1)
			Width=world.maxx
			Height=world.maxy
		if(!Width) return
		if(!Height) Height=Width
		var/turf/T
		var/X
		var/Y
		var/i
		var/atom/A
		var/Text=""
		Map.len=0
		Map.len=Height
		for(Y=0,Y<Height,Y++)

			for(X=0,X<Width,X++)
				T=locate(Loc.x+X,Loc.y+Y,Loc.z)

				Text="[Text][ascii2text(Index(T))]"
				for(i=1,i<=T.contents.len,i++)
					A=T.contents[i]
					if(A.A_DoNotSave) continue
					Text="[Text][ascii2text(Index(A))]"

			Map[Y+1]=Text
			Text=""







